package com.suben.goodsservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suben.common.bean.R;
import com.suben.goodsservice.entity.GoodsSpu;
import com.suben.goodsservice.entity.query.GoodsSpuQuery;
import com.suben.goodsservice.service.GoodsSpuService;
import com.suben.goodsservice.utils.SysUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Suben
 * @since 2021-05-09
 */
@Api(tags = {"商品接口"})
@RestController
@RequestMapping("/goodsSpuService/goods-spu")
@CrossOrigin
public class GoodsSpuController {

    @Autowired
    private GoodsSpuService goodsSpuService;

    @ApiOperation(value = "新增接口")
    @PutMapping("add")
    public R add(@RequestBody GoodsSpu goods) {
        // 通过商品名称生成spuCode
        goods.setSpuCode(SysUtils.getUniqueNumber());
        boolean save = goodsSpuService.save(goods);
        if (save) {
            return R.ok().message("新增商品成功~");
        }
        return R.error().message("新增商品失败~");
    }

    @ApiOperation(value = "更新接口")
    @PostMapping("update")
    public R update(@RequestBody GoodsSpu goods) {
        boolean save = goodsSpuService.saveOrUpdate(goods);
        if (save) {
            return R.ok().message("更新商品成功~");
        }
        return R.error().message("更新商品失败~");
    }

    @ApiOperation(value = "分页查询接口")
    @PostMapping("pageQueryCondition/{current}/{limit}")
    public R pageQueryCondition(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "goodsQuery", value = "查询对象", required = false)
            @RequestBody(required = false) GoodsSpuQuery goodsQuery) {

        //1、创建page对象
        Page<GoodsSpu> page = new Page<>(current,limit);
        // 不带条件进行分页查询
        if (goodsQuery == null){
            //注意：调用方法时，底层封装，把分页所有的数据都封装到了page对象中
            IPage<GoodsSpu> goodsIPage = goodsSpuService.page(page, null);//null所表示没有添加任何查询条件
            return R.ok().data("total",goodsIPage.getTotal()).data("rows",goodsIPage.getRecords());
        }
        //2、带条件查询并构建条件
        QueryWrapper<GoodsSpu> wrapper = new QueryWrapper<>();
        //3、多条件组合查询
        String name = goodsQuery.getName();
        String spuCode = goodsQuery.getSpuCode();
        Date gmtCreate = goodsQuery.getGmtCreate();
        //第一参数是数据库表中的字段，不是实体类的属性
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(spuCode)) {
            wrapper.like("spu_code",spuCode);
        }
        if(!StringUtils.isEmpty(gmtCreate)) {
            wrapper.like("gmt_create",gmtCreate);
        }
        //调用方法实现条件查询分页
        IPage<GoodsSpu> goodsIPage = goodsSpuService.page(page, wrapper);

        long total = goodsIPage.getTotal();//总记录数
        List<GoodsSpu> records = goodsIPage.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    // 根据id查询科技资讯
    @ApiOperation(value = "根据id查询商品详情")
    @GetMapping("getById/{id}")
    public R getById(
            @ApiParam(name = "id",value = "商品ID",required = true)
            @PathVariable String id){
        GoodsSpu goods = goodsSpuService.getById(id);
        return R.ok().data("item",goods);
    }

    @ApiOperation(value = "根据id删除商品详情")
    @DeleteMapping("deleteById/{id}")
    public R delete(
            @ApiParam(name = "id",value = "商品ID",required = true)
            @PathVariable String id){
        boolean remove = goodsSpuService.removeById(id);
        if (remove){ // 删除不存在的逻辑上属于成功
            return R.ok().message("逻辑删除成功~");
        }
        return R.error().message("删除失败~");
    }
}

